"use strict";

/**
 * vtuzx方法类
 */
//------------------------------------------------------------------
import dayjs from "dayjs";
//------------------------------------------------------------------

//字符串格式化
String.prototype.format = function () {
    var values = arguments;
    return this.replace(/\{(\d+)\}/g, function (match, index) {
        if (values.length > index) {
            return values[index];
        } else {
            return "";
        }
    });
};

/**
 * 范围随机数
 *
 * @param from 随机数范围from
 * @param to 随机数范围to
 */
const $randomRange = (from, to) => {
    return Math.floor(Math.random() * (to - from) + from);
};

/**
 * 格式化日期
 *
 * @param date 日期
 * @param format 格式
 */
const $formatDate = (date, format) => {
    if (!date) return "";
    return dayjs(date).format(format);
};

/**
 * 格式化数字
 * @param value
 * @param fixed
 * @returns {string}
 */
const $formatMoney = (value, fixed = 0) => {
    return parseFloat(value).toFixed(fixed)
}

/**
 * 格式化金额（分）
 * @param value
 * @param fixed
 * @returns {string|number}
 */
const $formatFenMoney = (value = 0, fixed = 0,) => {
    if (isNaN(value)) return 0
    return $formatMoney(value / 100, fixed)
}

/**
 * 格式化时间
 * @param time
 * @param second
 */
const $formatDateStr = (time, second) => {
    var now = new Date;
    var that = new Date(time);
    if (that.getFullYear() == now.getFullYear()
        && that.getMonth() == now.getMonth()
        && that.getDate() == now.getDate() - 1) {
        return '昨天 ' + dayjs(that).format('HH:mm' + (second ? ":ss" : ''));
    }
    if (that.getFullYear() == now.getFullYear()
        && that.getMonth() == now.getMonth()
        && that.getDate() == now.getDate()) {
        return '今天 ' + dayjs(that).format('HH:mm' + (second ? ":ss" : ''));
    }
    if (that.getFullYear() == now.getFullYear()
        && that.getMonth() == now.getMonth()
        && that.getDate() == now.getDate() - 2) {
        return '前天 ' + dayjs(that).format('HH:mm' + (second ? ":ss" : ''));
    }

    if (that.getFullYear() == now.getFullYear()
        && that.getMonth() == now.getMonth()
        && that.getDate() != now.getDate()) {
        return dayjs(that).format('MM-DD');
    }

    if (that.getFullYear() == now.getFullYear()
        && that.getMonth() != now.getMonth()) {
        return dayjs(that).format('MM-DD HH:mm' + (second ? ":ss" : ''));
    }

    if (that.getFullYear() != now.getFullYear()) {
        return dayjs(that).format('YYYY-MM-DD HH:mm' + (second ? ":ss" : ''));
    }
    return dayjs(that).format('YYYY-MM-DD HH:mm' + (second ? ":ss" : ''));
}

/**
 * 校验手机号
 * @param phone
 * @returns {boolean}
 */
const $checkPhone = (phone) => {
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
        return false;
    }
    return true
}

/**
 * 解析URL参数
 * @param url
 * @returns {*[]}
 */
const $getQueryVariable = (url) => {
    var params = {}, h;
    var hash = url.slice(url.indexOf("?") + 1).split('&');
    for (var i = 0; i < hash.length; i++) {
        h = hash[i].split("="); //
        params[h[0]] = h[1];
    }
    return params;
}

/**
 * 下载处理
 * @param param 参数
 */
const $download = param => {
    let link = document.createElement("a");
    try {
        link.href = window.URL.createObjectURL(param.data);
        link.download = param.filename;
        document.body.appendChild(link);
        link.click();
    } finally {
        document.body.removeChild(link);
    }
};

/**
 * 列表转对象
 *
 * @param list 列表
 * @param keyCol key字段
 * @param valCol 值字段，不设定时保存对象本身
 */
const $list2Map = (list, keyCol, valCol) => {
    let res = {};
    if (!list || list.length === 0) return res;
    for(let i = 0; i < list.length; i++) {
        res[list[i][keyCol]] = valCol ? list[i][valCol] : list[i];
    }
    return res;
}

export default {
    randomRange: $randomRange,
    formatFenMoney: $formatFenMoney,
    formatDate: $formatDate,
    formatDateStr: $formatDateStr,
    formatMoney: $formatMoney,
    checkPhone: $checkPhone,
    getQueryVariable: $getQueryVariable,
    download: $download,
    list2Map: $list2Map
};
